package com.dc.chat.app.biz.impl;

import com.dc.chat.app.biz.FeedBackBiz;
import com.dc.chat.app.service.mp.FeedBackService;
import com.dc.chat.app.service.mp.UserService;
import com.dc.chat.entity.dao.FeedBack;
import com.dc.chat.entity.dao.User;
import com.dc.chat.entity.to.ResponseBean;
import com.dc.chat.interceptor.security.SecurityInterceptor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service
public class FeedBackBizImpl implements FeedBackBiz {
    @Resource
    FeedBackService feedBackService;
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    SimpMessagingTemplate simpMessagingTemplate;
    @Resource
    UserService userService;
    @Override
    public ResponseBean<Integer> addFeedBack(String feedBackString) {
        String username = SecurityInterceptor.threadLocal.get();
        User user = userService.selectByUserName(username);
        FeedBack feedBack=new FeedBack();
        feedBack.setCreateTime(LocalDateTime.now());
        feedBack.setDeleted(0);
        feedBack.setUserId(user.getId());
        feedBack.setStatus(0);
        feedBack.setFeedBack(feedBackString);
        boolean save = feedBackService.save(feedBack);
        if(save){
            //通知各个管理员  该刷新界面了
            Set<String> adminTokens = redisTemplate.opsForSet().members("admin");
            for (String adminToken : adminTokens) {
                Map map=new HashMap();
                map.put("refreshFeedBack",true);
                simpMessagingTemplate.convertAndSend("/back/"+adminToken,"",map);
            }
            return ResponseBean.success("反馈成功",feedBack.getId());
        }else{
            return ResponseBean.failed("反馈失败");
        }
    }
}
